package wallet

import (
	"fmt"
	"jgt-sdk/utils"
)

type wechatapp struct{}

type WechatAppCheckResult struct {
	Result string `json:"result"`

	RecordID string `json:"record_id"`
	OrderID  string `json:"order_id"`
	Price    int64  `json:"price"`
	Msg      string `json:"msg"`
}

// 支付
func (a *wechatapp) pay(h *Handle, params *PayParams) (map[string]string, error) {
	uslStr, err := h.GetURL(fmt.Sprintf("/srv/v1/wechat/app/%s/pay", params.Client))
	if err != nil {
		return nil, err
	}
	body := map[string]interface{}{
		"order_id": params.OrderId,
		"describe": params.Describe,
		"price":    params.Price,
		"tag":      params.Tag,
	}
	var result = map[string]string{}

	err = utils.PostJSONWithToken(h.getContext(), uslStr, h.as.GetTokenHandle().Get, body, &result)

	return result, err
}

// 同步回调
func (a *wechatapp) check(h *Handle, params *CheckParams) (*CheckResult, error) {
	result := new(CheckResult)
	uslStr, err := h.GetURL(fmt.Sprintf("/srv/v1/alipay/app/%s/check/%s", params.Client, params.OrderId))
	if err != nil {
		return result, err
	}
	data := new(WechatAppCheckResult)
	err = utils.PostJSONWithToken(h.getContext(), uslStr, h.as.GetTokenHandle().Get, nil, data)
	if err != nil {
		return result, err
	}
	result.Result = data.Result
	result.Msg = data.Msg
	result.BillID = data.RecordID
	result.OrderID = data.OrderID
	result.Price = data.Price

	return result, err
}
